#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time       :   11:42
# Author     : Y4
# Site       : 
# File       : client_user_model.py
from werkzeug.security import generate_password_hash, check_password_hash

from App.common.utils.constant import COMMON_USER, BLACK_USER
from App.models import BaseModel
from App.plugins import models


class User(BaseModel):
    __bind_key__ = 'yjh_users'  # 绑定数据库

    username = models.Column(models.String(32), unique=True)  # 用户名
    _password = models.Column(models.String(256))  # 密码
    phone = models.Column(models.String(32), unique=True)  # 手机
    is_delete = models.Column(models.Boolean, default=False)  # 是否删除(默认否)
    permission = models.Column(models.Integer, default=COMMON_USER)  # 用户权限(默认普通用户)


    @property
    def password(self):
        raise Exception('密码不能访问')

    @password.setter
    def password(self, value):
        self._password = generate_password_hash(value)

    def check_password(self, value):
        return check_password_hash(self._password, value)

    def check_permission(self, permission):
        if permission & self.permission == BLACK_USER:
            return False
        else:
            return permission & self.permission == permission